package com.wxy.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wxy.web.mapper.UserMapper;
import com.wxy.web.model.User;
import com.wxy.web.service.IMenuService;
import com.wxy.web.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wxy
 * @since 2021-06-13
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private IMenuService menuService;
    public HashMap<String, Object> detail(String username) {
        HashMap<String,Object> map = new HashMap<>();
        User user = this.getByUsername(username);

        map.put("id",user.getId());
        map.put("username",username);

        map.put("menus",menuService.listByUserId(user.getId()));
        //map.put("menus",menuService.listAll());
        List<String> list = new ArrayList<>();
        list.add("admin");
        list.add("user");
        map.put("roles",list);

        return map;
    }

    public List<User> listByRoleId(Integer roleId,Integer pageNum,Integer pageSize)
    {
        return this.getBaseMapper().listByRoleId(roleId,(pageNum-1)*pageSize,pageSize);
    }

    public Integer countByRoleId(Integer roleId)
    {
        return this.getBaseMapper().countByRoleId(roleId);
    }

    public Boolean insertUserRole(Integer userId,Integer roleId){
        return this.getBaseMapper().insertUserRole(userId,roleId);
    }

    public Integer deleteRoleByUserId(Integer userId){
        return this.getBaseMapper().deleteRoleByUserId(userId);
    }

    public User getByUsername(String username){
        LambdaQueryWrapper<User> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(User::getUsername , username);
        return this.baseMapper.selectOne(lambdaQueryWrapper);
    }
}
